﻿
using System;
using System.Collections.Generic;
using System.Text;
using Mang24k99.Model.CLMN;
using NHibernate;
using NHibernate.Expression;
using Castle.ActiveRecord.Queries;
using Common;
namespace Mang24k99.Business.CLMN
{
    public partial class BKtClmnItems
    {
        /// <summary>
        /// Finds the by layer.通用栏目
        /// </summary>
        /// <param name="ColumnLayerValue">The column layer value.</param>
        /// <param name="ColumnNoParentValue">The column no parent value.</param>
        /// <returns></returns>
        public static IList<MKtClmnItems> FindAllByLayerGeneral(int? ColumnLayerValue, int? ColumnNoParentValue, int? findLayer)
        {
            ICriterion[] criteria = GetQueryCondition(ref ColumnLayerValue, ref ColumnNoParentValue, findLayer);
            return MKtClmnItems.SelectAll(criteria);
        }
        /// <summary>
        /// Finds the by layer DDL list.(下拉列表用,用户维护界面)
        /// </summary>
        /// <param name="ColumnLayerValue">The column layer value.</param>
        /// <param name="ColumnNoParentValue">The column no parent value.</param>
        /// <returns></returns>
        public static IList<MKtClmnItems> FindAllByLayerDdlList(int? ColumnLayerValue, int? ColumnNoParentValue)
        {
            ICriterion[] criteria = GetQueryCondition(ref ColumnLayerValue, ref ColumnNoParentValue, null);
            criteria = Common.ArrayHelper<ICriterion>.Merge(new ICriterion[][] { 
                            criteria,
                            new ICriterion[]{Expression.In("ColumnAttrib",new object[]{1,99})}, 
                            //为1的话是挂接好译星
                            new ICriterion[]{Expression.In("IsReltrastar",new object[]{1})}, 
                            new ICriterion[]{Expression.In("TrastarNum",new object[]{0})}, 
                            
                            });
            Order[] order = GetOrder();
            return MKtClmnItems.SelectAll(order, criteria);
        }
        /// <summary>
        /// Finds the by layer.(列表普通栏目,用户界面用)
        /// </summary>
        /// <param name="ColumnLayerValue">The column layer value.</param>
        /// <param name="ColumnNoParentValue">The column no parent value.</param>
        /// <returns></returns>
        public static IList<MKtClmnItems> FindAllByLayer(int? ColumnLayerValue, int? ColumnNoParentValue)
        {
            string parentName = GetParentName(ColumnLayerValue, ColumnNoParentValue);
            if(parentName == null)
            {
                return null;
            }
            string strHql = @"Select MKtClmnItems"
                        + " from"
                        + " MKtClmnItems MKtClmnItems"
                        + " where"
                        + " MKtClmnItems.IsValidate = 1"
                        + " and MKtClmnItems." + parentName + " in (:ColumnNofirst)"
                        + " and MKtClmnItems.ColumnAttrib in (:ColumnAttrib)"
                        + " and MKtClmnItems.ColumnLayer in (:ColumnLayer)"
                        + " order by"
                        + " case  when MKtClmnItems.ChannelNO is null then 65535 else MKtClmnItems.ChannelNO end,"
                        + " MKtClmnItems.ColumnNO"
                       ;
            SimpleQuery query = new SimpleQuery(typeof(MKtClmnItems), strHql);
            query.SetParameter("ColumnLayer", ColumnLayerValue);
            query.SetParameter("ColumnNofirst", ColumnNoParentValue);
            query.SetParameterList("ColumnAttrib", new int[] { 1, 99 });
            return MKtClmnItems.SelectAllQuery(query);
        }
        /// <summary>
        /// Finds the by layer DDL list.(下拉列表,通用栏目用)
        /// </summary>
        /// <param name="ColumnLayerValue">The column layer value.</param>
        /// <param name="ColumnNoParentValue">The column no parent value.</param>
        /// <returns></returns>
        public static IList<MKtClmnItems> FindAllByLayerDdlListGeneral(int? ColumnLayerValue, int? ColumnNoParentValue)
        {

            ICriterion[] criteria = GetQueryCondition(ref ColumnLayerValue, ref ColumnNoParentValue, null);
            criteria = Common.ArrayHelper<ICriterion>.Merge(new ICriterion[][] { 
                            criteria,
                            new ICriterion[]{Expression.In("ColumnAttrib",new object[]{1,99})}, 
                            
                            });
            Order[] order = GetOrder();
            return MKtClmnItems.SelectAll(order, criteria);
        }

        /// <summary>
        /// Finds the by layer only.(列表,内页排序用)
        /// </summary>
        /// <param name="ColumnLayerValue">The column layer value.</param>
        /// <param name="ColumnNoParentValue">The column no parent value.</param>
        /// <returns></returns>
        public static IList<MKtClmnItems> FindAllByLayerOrder(int? ColumnLayerValue, int? ColumnNoParentValue)
        {
            string parentName = GetParentName(ColumnLayerValue, ColumnNoParentValue);
            if(parentName == null)
            {
                return null;
            }
            string strHql = @"Select MKtClmnItems"
                        + " from"
                        + " MKtClmnItems MKtClmnItems"
                        + " where"
                        + " MKtClmnItems.IsValidate = 1"
                        + " and MKtClmnItems." + parentName + " in (:ColumnNofirst)"
                        + " and MKtClmnItems.ColumnAttrib in (:ColumnAttrib)"
                        + " and MKtClmnItems.ColumnLayer in (:ColumnLayer)"
                        + " and MKtClmnItems.IsShow = 1"
                        + " order by"
                        + " case  when MKtClmnItems.ChannelNO is null then 65535 else MKtClmnItems.ChannelNO end,"
                        + " MKtClmnItems.ColumnNO"
                       ;

            SimpleQuery query = new SimpleQuery(typeof(MKtClmnItems), strHql);
            query.SetParameter("ColumnLayer", ColumnLayerValue);
            query.SetParameter("ColumnNofirst", ColumnNoParentValue);
            query.SetParameterList("ColumnAttrib", new int[] { 1, 99 });

            return MKtClmnItems.SelectAllQuery(query);

        }

        /// <summary>
        /// Finds the by layer main page.(首页排序)
        /// </summary>
        /// <param name="layer">The layer.</param>
        /// <param name="parentNo">The parent no.</param>
        /// <returns></returns>
        public static IList<MKtClmnItems> FindAllByLayerMainPage(int layer, int[] ColumnNofirst)
        {
            string strHql = @"Select MKtClmnItems"
             + " from"
             + " MKtClmnItems MKtClmnItems"
             + " where"
             + " MKtClmnItems.IsValidate = 1"
             + " and MKtClmnItems.ColumnNofirst in (:ColumnNofirst)"
             + " and MKtClmnItems.ColumnAttrib in (:ColumnAttrib)"
             + " and MKtClmnItems.ChildNum = 0"
             + " and MKtClmnItems.IsShow = 1"
             + " and MKtClmnItems.IsReltrastar = 1"
            + " order by"
            + " case  when MKtClmnItems.MainpageNO is null then 65535 else MKtClmnItems.MainpageNO end,"
            + " MKtClmnItems.ColumnNO"

             ;
            SimpleQuery query = new SimpleQuery(typeof(MKtClmnItems), strHql);
            query.SetParameterList("ColumnNofirst", ColumnNofirst);
            query.SetParameterList("ColumnAttrib", new int[] { 1, 99 });


            return MKtClmnItems.SelectAllQuery(query);

        }

        /// <summary>
        /// Finds the by layer main page.(首页排序,只对所有评论排序)
        /// </summary>
        /// <param name="layer">The layer.</param>
        /// <param name="parentNo">The parent no.</param>
        /// <returns></returns>
        public static IList<MKtClmnItems> FindAllByLayerMainPageAllComment(int layer, int[] ColumnNofirst)
        {
            string strHql = @"Select MKtClmnItems"
             + " from"
             + " MKtClmnItems MKtClmnItems"
             + " where"
             + " MKtClmnItems.IsValidate = 1"
             + " and MKtClmnItems.ColumnNofirst in (:ColumnNofirst)"
             + " and MKtClmnItems.ColmunNosecond in (11)"//所有评论
             + " and MKtClmnItems.ColumnAttrib in (:ColumnAttrib)"
             + " and MKtClmnItems.ChildNum = 0"
             + " and MKtClmnItems.IsShow = 1"
            + " order by"
            + " case  when MKtClmnItems.MainpageNO is null then 65535 else MKtClmnItems.MainpageNO end,"
            + " MKtClmnItems.ColumnNO"

             ;
            SimpleQuery query = new SimpleQuery(typeof(MKtClmnItems), strHql);
            query.SetParameterList("ColumnNofirst", ColumnNofirst);
            query.SetParameterList("ColumnAttrib", new int[] { 1, 99 });


            return MKtClmnItems.SelectAllQuery(query);

        }




        /// <summary>
        /// Gets the order.
        /// </summary>
        /// <returns></returns>
        private static Order[] GetOrder()
        {
            Order[] order = MKtClmnItems.Asc(new string[] { "ChannelNO", "ColumnNO" });
            return order;
        }
        /// <summary>
        /// Gets the name of the parent.(获取父栏目的名称)
        /// </summary>
        /// <param name="ColumnLayerValue">The column layer value.</param>
        /// <param name="ColumnNoParentValue">The column no parent value.</param>
        /// <returns></returns>
        private static string GetParentName(int? ColumnLayerValue, int? ColumnNoParentValue)
        {
            string ColumnNoParentName = null;
            switch(ColumnLayerValue)
            {
                case 2:
                    ColumnNoParentName = "ColumnNofirst";
                    break;
                case 3:
                    ColumnNoParentName = "ColmunNosecond";
                    break;
                case 4:
                    ColumnNoParentName = "ColumnNothird";
                    break;
                default:
                    break;
            }
            return ColumnNoParentName;
        }
        /// <summary>
        /// Gets the query condition.
        /// </summary>
        /// <param name="ColumnLayerValue">The column layer value.</param>
        /// <param name="ColumnNoParentValue">The column no parent value.</param>
        /// <param name="findLayer">The find layer.要查找的层</param>
        /// <returns></returns>
        private static ICriterion[] GetQueryCondition(ref int? ColumnLayerValue, ref int? ColumnNoParentValue, int? findLayer)
        {


            string ColumnNoParentName = null;
            switch(ColumnLayerValue)
            {
                case 2:
                    ColumnNoParentName = "ColumnNofirst";
                    break;
                case 3:
                    ColumnNoParentName = "ColmunNosecond";
                    break;
                case 4:
                    ColumnNoParentName = "ColumnNothird";
                    break;
                case 1:
                default:
                    ColumnNoParentName = "ColumnNofirst";
                    if(findLayer == 1)
                    {
                        ColumnNoParentValue = 0;
                        ColumnLayerValue = 1;
                    }

                    break;
            }
            if(findLayer == null)
            {
                findLayer = ColumnLayerValue;
            }
            ICriterion[] criteria = new ICriterion[]
            {
                Expression.Eq("IsValidate",1),
                Expression.Eq("ColumnLayer",findLayer),
                Expression.In("ColumnAttrib",new int[]{1,99}),
            };
            if(ColumnNoParentValue != 0)//
            {
                criteria = Common.ArrayHelper<ICriterion>.Merge(new ICriterion[][] { 
                            criteria,
                            new ICriterion[]{Expression.Eq(ColumnNoParentName,ColumnNoParentValue),} 
                            });
            }
            return criteria;
        }
        /// <summary>
        /// News the ID.
        /// </summary>
        /// <returns></returns>
        public static int NewID()
        {
            string strHql = @"Select Max(MKtClmnItems.ColumnNO)"
                          + " from"
                          + " MKtClmnItems MKtClmnItems"
                          ;
            ScalarQuery query = new ScalarQuery(typeof(MKtClmnItems), strHql);
            object obj = MKtClmnItems.SelectScalarQuery(query);
            int result = 0;
            if(obj != null)
            {
                result = (int)obj;
            }
            result += 1;
            return result;
        }
        /// <summary>
        /// Existses the specified column layer value.
        /// </summary>
        /// <param name="ColumnLayerValue">The column layer value.</param>
        /// <param name="ColumnNoParentValue">The column no parent value.</param>
        /// <param name="ColumnName">Name of the column.</param>
        /// <param name="ColumnNameValue">The column name value.</param>
        /// <param name="ColumnNo">The column no.</param>
        /// <returns></returns>
        private static bool Exists(int? ColumnLayerValue, int? ColumnNoParentValue, string ColumnName, string ColumnNameValue, int? ColumnNo)
        {

            ICriterion[] criteria = GetQueryCondition(ref ColumnLayerValue, ref ColumnNoParentValue, null);

            criteria = Common.ArrayHelper<ICriterion>.Merge(new ICriterion[][] { 
                            criteria,
                            new ICriterion[]{
                                Expression.Eq(ColumnName ,ColumnNameValue),
                                Expression.Not(Expression.Eq("ColumnNO",ColumnNo)),
                            
                            } 
                            });
            return MKtClmnItems.Exist(criteria);
        }
        /// <summary>
        /// Exists the name of the by.
        /// </summary>
        /// <param name="ColumnLayerValue">The column layer value.</param>
        /// <param name="ColumnNoParentValue">The column no parent value.</param>
        /// <param name="ColumnNameValue">The column name value.</param>
        /// <param name="ColumnNo">The column no.</param>
        /// <returns></returns>
        public static bool ExistByName(int? ColumnLayerValue, int? ColumnNoParentValue, string ColumnNameValue, int? ColumnNo)
        {
            return Exists(ColumnLayerValue, ColumnNoParentValue, "ColumnName", ColumnNameValue, ColumnNo);
        }
        /// <summary>
        /// Exists the by alias.
        /// </summary>
        /// <param name="ColumnLayerValue">The column layer value.</param>
        /// <param name="ColumnNoParentValue">The column no parent value.</param>
        /// <param name="ColumnNameValue">The column name value.</param>
        /// <param name="ColumnNo">The column no.</param>
        /// <returns></returns>
        public static bool ExistByAlias(int? ColumnLayerValue, int? ColumnNoParentValue, string ColumnNameValue, int? ColumnNo)
        {
            return Exists(ColumnLayerValue, ColumnNoParentValue, "ColumnAlias", ColumnNameValue, ColumnNo);
        }

        /// <summary>
        /// Exists the by detailname.
        /// </summary>
        /// <param name="ColumnLayerValue">The column layer value.</param>
        /// <param name="ColumnNoParentValue">The column no parent value.</param>
        /// <param name="ColumnNameValue">The column name value.</param>
        /// <param name="ColumnNo">The column no.</param>
        /// <returns></returns>
        public static bool ExistByDetailname(int? ColumnLayerValue, int? ColumnNoParentValue, string ColumnNameValue, int? ColumnNo)
        {
            return Exists(ColumnLayerValue, ColumnNoParentValue, "ColumnDetailname", ColumnNameValue, ColumnNo);
        }

        /// <summary>
        /// Deletes the child.
        /// </summary>
        /// <param name="where">The where.</param>
        public static void DeleteChild(string where)
        {
            MKtClmnItems.Deletes(where);
        }

        /// <summary>
        /// Creates the before.
        /// </summary>
        /// <param name="model">The model.</param>
        /// <returns></returns>
        private static bool CreateBefore(MKtClmnItems model)
        {
            return false;
        }

        /// <summary>
        /// Creates the after.
        /// </summary>
        /// <param name="model">The model.</param>
        private static void CreateAfter(MKtClmnItems model)
        {
            MKtClmnItems parentModel = null;
            switch(model.ColumnLayer)
            {

                case 2:
                    parentModel = Find(model.ColumnNofirst);
                    break;
                case 3:
                    parentModel = Find(model.ColmunNosecond);
                    break;
                case 4:
                    parentModel = Find(model.ColumnNothird);
                    break;
                default:
                    break;
            }
            if(parentModel != null)
            {
                parentModel.ChildNum += 1;
                Update(parentModel);
            }
        }
        /// <summary>
        /// Updates the before.
        /// </summary>
        /// <param name="model">The model.</param>
        /// <returns></returns>
        private static bool UpdateBefore(MKtClmnItems model)
        {
            return false;
        }
        /// <summary>
        /// Updates the after.
        /// </summary>
        /// <param name="model">The model.</param>
        private static void UpdateAfter(MKtClmnItems model)
        {
            if(model.LastNum1 == null || model.LastNum2 == null || model.LastNum3 == null)
            {
                return;
            }
            MKtClmnItems parentModel = null;
            MKtClmnItems parentModelLast = null;

            switch(model.ColumnLayer)
            {


                case 2:
                    if(model.LastNum1 != model.ColumnNofirst)
                    {
                        parentModel = Find(model.ColumnNofirst);
                        parentModelLast = Find(model.LastNum1);
                    }


                    break;
                case 3:
                    if(model.LastNum2 != model.ColmunNosecond)
                    {
                        parentModel = Find(model.ColmunNosecond);
                        parentModelLast = Find(model.LastNum2);
                    }
                    break;
                case 4:
                    if(model.LastNum3 != model.ColumnNothird)
                    {
                        parentModel = Find(model.ColumnNothird);
                        parentModelLast = Find(model.LastNum3);
                    }
                    break;
                default:
                    break;
            }
            if(parentModel != null)
            {
                parentModel.ChildNum += 1;
                Update(parentModel);
            }
            if(parentModelLast != null)
            {
                parentModelLast.ChildNum -= 1;
                Update(parentModelLast);
            }
        }

        /// <summary>
        /// Deletes the before.
        /// </summary>
        /// <param name="model">The model.</param>
        /// <returns></returns>
        private static bool DeleteBefore(MKtClmnItems model)
        {
            return false;
        }
        /// <summary>
        /// Deletes the after.
        /// </summary>
        /// <param name="model">The model.</param>
        private static void DeleteAfter(MKtClmnItems model)
        {
            MKtClmnItems parentModel = null;
            int? parentNo = null;
            string parentColumnName = null;
            switch(model.ColumnLayer)
            {
                case 1:
                    parentColumnName = "Column_Nofirst";

                    break;
                case 2:
                    parentColumnName = "Colmun_Nosecond";
                    parentModel = Find(model.ColumnNofirst);
                    break;
                case 3:
                    parentColumnName = "Column_Nothird";
                    parentModel = Find(model.ColmunNosecond);
                    break;
                case 4:
                    parentModel = Find(model.ColumnNothird);
                    break;
                default:
                    break;
            }
            if(parentColumnName != null)
            {
                parentNo = model.ColumnNO;
                DeleteChild(parentColumnName + " = " + parentNo.Value);
            }
            if(parentModel != null)
            {
                parentModel.ChildNum -= 1;
                if(parentModel.ChildNum < 0)
                {
                    parentModel.ChildNum = 0;
                }
                Update(parentModel);
            }
        }

        /// <summary>
        /// 选出所有叶子节点的栏目
        /// </summary>
        /// <returns></returns>
        public static IList<MKtClmnItems> FindAllChild()
        {
            ICriterion[] criteria = new ICriterion[]
            {
                Expression.Eq("IsValidate",1),
                Expression.Eq("ChildNum",0),
                Expression.In("ColumnAttrib",new int[]{1,99}),
                Expression.Eq("IsReltrastar",1),
                Expression.Eq("ColmunNosecond",11),

            };
            Order[] order1 = MKtClmnItems.Asc("ColumnNofirst", "ColmunNosecond", "ColumnNothird", "ChannelNO");

            return MKtClmnItems.SelectAll(order1, criteria);
        }
        /// <summary>
        /// 选出所有叶子节点的栏目为ddl控件
        /// </summary>
        /// <returns></returns>
        public static IList<MKtClmnItems> FindAllChildForDDL()
        {

            IList<MKtClmnItems> ls = FindAllChild();
            foreach(MKtClmnItems item in ls)
            {
                if(item.ColumnLayer == 1)
                {
                    //不做处理
                }
                else if(item.ColumnLayer == 2)
                {
                    //不做处理
                    MKtClmnItems item1 = BKtClmnItems.Find(item.ColumnNofirst);
                    item.ColumnName = item1.ColumnName + ">>" + item.ColumnName;
                }
                else if(item.ColumnLayer == 3)
                {
                    MKtClmnItems item1 = BKtClmnItems.Find(item.ColumnNofirst);
                    MKtClmnItems item2 = BKtClmnItems.Find(item.ColmunNosecond);
                    item.ColumnName = item1.ColumnName
                        + ">>" + item2.ColumnName
                        + ">>" + item.ColumnName;
                }
                else if(item.ColumnLayer == 4)
                {
                    MKtClmnItems item1 = BKtClmnItems.Find(item.ColumnNofirst);
                    MKtClmnItems item2 = BKtClmnItems.Find(item.ColmunNosecond);
                    MKtClmnItems item3 = BKtClmnItems.Find(item.ColumnNothird);
                    item.ColumnName = item1.ColumnName
                        + ">>" + item2.ColumnName
                        + ">>" + item3.ColumnName
                        + ">>" + item.ColumnName;
                }
            }
            return ls;
        }



    }
}
